package page.mainPage;

import cn.leancloud.LCUser;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import page.BasePage;
import utils.ControllerUtils;
import utils.DBUtils;
import utils.GlobalVar;

import java.io.IOException;

public class LoginPage extends BasePage {
    static double oldStageX;
    static double oldStageY;
    static double oldScreenX;
    static double oldScreenY;

    @FXML
    private Button loginButton;

    @FXML
    private ImageView closeImage;

    @FXML
    private TextField passwordTextField;

    @FXML
    private TextField usernameTextField;

    @FXML
    public void initialize() {
        closeImage.setOnMouseClicked(mouseEvent -> ControllerUtils.closePane(root));
        usernameTextField.setPromptText("账号");
        passwordTextField.setPromptText("密码");
        loginButton.setOnMouseClicked(mouseEvent -> {
            String username = usernameTextField.getText();
            String password = passwordTextField.getText();
            DBUtils dbUtils = new DBUtils();
            if (dbUtils.loginWithPassword(username, password)) {
                ((Stage) root.getScene().getWindow()).close();
                // 打开主页
                FXMLLoader loader = ControllerUtils.getLoaderFromFxml(getClass().getResource("MainPage.fxml"));
                GlobalVar.loader = loader;
                try {
                    Parent parent = loader.load();
                    Scene scene = new Scene(parent);
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    stage.show();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                String session = LCUser.getCurrentUser().getSessionToken();
                ControllerUtils.writeSession(session);
            } else {
                System.out.println("账户或错误，登陆失败");
            }

        });
    }

    public static Scene createScene(FXMLLoader loader) {
        Parent root;
        try {
            root = loader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Scene scene = new Scene(root);
        //鼠标按下事件
        scene.setOnMousePressed(event -> {
            Stage stage = (Stage) scene.getWindow();
            oldStageX = stage.getX();
            oldStageY = stage.getY();
            oldScreenX = event.getScreenX();
            oldScreenY = event.getScreenY();
        });

        //鼠标拖拽
        scene.setOnMouseDragged(event -> {
            //新位置
            //拖拽前后的鼠标差值加上原始窗体坐标值
            Stage stage = (Stage) scene.getWindow();
            stage.setX(event.getScreenX() - oldScreenX + oldStageX);
            stage.setY(event.getScreenY() - oldScreenY + oldStageY);
        });
        return scene;
    }


}
